/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package me.drakeet.multitype.sample.slice;

import me.drakeet.multitype.sample.ResourceTable;

import me.drakeet.multitype.sample.communicatewithbinder.SimpleSlice;
import me.drakeet.multitype.sample.moreapis.MoreApisPlaygoundSlice;
import me.drakeet.multitype.sample.normal.NormalSlice;
import me.drakeet.multitype.sample.utils.ViewUtil;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import me.drakeet.multitype.sample.payload.TestPayloadSlice;
import ohos.agp.components.Button;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.LayoutScatter;

/**
 * MainAbilitySlice
 */
public class MainAbilitySlice extends AbilitySlice {
    private ComponentContainer rootContainer;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        rootContainer = (ComponentContainer) LayoutScatter.getInstance(this)
                .parse(ResourceTable.Layout_slice_main, null, false);
        super.setUIContent(rootContainer);
    }

    @Override
    public void onActive() {
        super.onActive();

        if (rootContainer.findComponentById(ResourceTable.Id_btn_communicate_with_binder) instanceof Button) {
            Button btnCommunicate = (Button) rootContainer.findComponentById(ResourceTable.Id_btn_communicate_with_binder);
            btnCommunicate.setBackground(ViewUtil.customElement());
            btnCommunicate.setClickedListener(listener -> present(new SimpleSlice(), new Intent()));
        }

        if (rootContainer.findComponentById(ResourceTable.Id_btn_normal) instanceof Button) {
            Button btnNormal = (Button) rootContainer.findComponentById(ResourceTable.Id_btn_normal);
            btnNormal.setBackground(ViewUtil.customElement());
            btnNormal.setClickedListener(listener -> present(new NormalSlice(), new Intent()));
        }

        if (rootContainer.findComponentById(ResourceTable.Id_btn_test_payload) instanceof Button) {
            Button btnTestPayload = (Button) rootContainer.findComponentById(ResourceTable.Id_btn_test_payload);
            btnTestPayload.setBackground(ViewUtil.customElement());
            btnTestPayload.setClickedListener(listener -> present(new TestPayloadSlice(), new Intent()));
        }

        if (rootContainer.findComponentById(ResourceTable.Id_btn_observable) instanceof Button) {
            Button btnObservable = (Button) rootContainer.findComponentById(ResourceTable.Id_btn_observable);
            btnObservable.setBackground(ViewUtil.customElement());
            btnObservable.setClickedListener(listener -> present(new MoreApisPlaygoundSlice(), new Intent()));
        }
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

}